/*---------------------------------------------------------------------------------------------------------------------------------------------------------------*/
/*-----------------------------------------------------------------------Sites------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------------------------------------------------------------------------------------------*/
CREATE TABLE [dbo].[Sites](
	[Id] [int] IDENTITY(1,1) NOT NULL,
	[Name] [nvarchar](255) NOT NULL,
	[Description] [nvarchar](255) NULL,
	[Alias] [nvarchar](255) NOT NULL,
	[DefaultPage] [int] NOT NULL,
	[Skin] [nvarchar](255) NOT NULL,
	[RegistrationType] [int] NOT NULL,
	[Culture] [nvarchar](255) NOT NULL,
	[CreatedDate] [datetime] NOT NULL,
	[ModifiedDate] [datetime] NOT NULL,
 CONSTRAINT [PK_me_Sites] PRIMARY KEY CLUSTERED 
(
	[Id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

/*---------------------------------------------------------------------------------------------------------------------------------------------------------------*/
/*-----------------------------------------------------------------------SiteSettings------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------------------------------------------------------------------------------------------*/
CREATE TABLE [dbo].[SiteSettings](
	[Id] [int] IDENTITY(1,1) NOT NULL,
	[SiteId] [int] NOT NULL,
	[Key] [nvarchar](255) NOT NULL,
	[Value] [nvarchar](max) NOT NULL,
	[CreatedDate] [datetime] NOT NULL,
	[ModifiedDate] [datetime] NOT NULL,
 CONSTRAINT [PK_SiteSettings] PRIMARY KEY CLUSTERED 
(
	[Id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO
ALTER TABLE [dbo].[SiteSettings]  WITH CHECK ADD  CONSTRAINT [FK_me_SiteSettings_me_Sites] FOREIGN KEY([SiteId])
REFERENCES [dbo].[Sites] ([Id])
GO
ALTER TABLE [dbo].[SiteSettings] CHECK CONSTRAINT [FK_me_SiteSettings_me_Sites]

GO
/*---------------------------------------------------------------------------------------------------------------------------------------------------------------*/
/*-----------------------------------------------------------------------[Users]------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------------------------------------------------------------------------------------------*/
CREATE TABLE [dbo].[Users](
	[Id] [int] IDENTITY(1,1) NOT NULL,
	[SiteId] [int] NOT NULL,
	[Name] [nvarchar](255) NOT NULL,
	[IsHost] [bit] NOT NULL,
	[Password] [nvarchar](128) NOT NULL,
	[PasswordSalt] [nvarchar](128) NOT NULL,
	[Email] [nvarchar](255) NOT NULL,
	[IsApproved] [bit] NOT NULL,
	[IsLockedOut] [bit] NOT NULL,
	[CreatedDate] [datetime] NOT NULL,
	[ModifiedDate] [datetime] NOT NULL,
	[FailedPasswordAttemptCount] [int] NOT NULL,
	[FailedPasswordAnswerAttemptCount] [int] NOT NULL,
	[CreateGuid] [uniqueidentifier] NULL,
	[RestoreGuid] [uniqueidentifier] NULL,
	[CreatedBy] [int] NULL,
	[ModifiedBy] [int] NULL,
 CONSTRAINT [PK_me_Users] PRIMARY KEY CLUSTERED 
(
	[Id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO
ALTER TABLE [dbo].[Users]  WITH CHECK ADD  CONSTRAINT [FK_me_Users_me_Sites] FOREIGN KEY([SiteId])
REFERENCES [dbo].[Sites] ([Id])
GO
ALTER TABLE [dbo].[Users] CHECK CONSTRAINT [FK_me_Users_me_Sites]

GO
/*---------------------------------------------------------------------------------------------------------------------------------------------------------------*/
/*-----------------------------------------------------------------------[UserSettings]------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------------------------------------------------------------------------------------------*/

CREATE TABLE [dbo].[UserSettings](
	[Id] [int] IDENTITY(1,1) NOT NULL,
	[UserId] [int] NOT NULL,
	[Key] [nvarchar](255) NOT NULL,
	[Value] [nvarchar](255) NULL,
	[CreatedDate] [datetime] NOT NULL,
	[ModifiedDate] [datetime] NOT NULL,
 CONSTRAINT [PK_me_UserSettings] PRIMARY KEY CLUSTERED 
(
	[Id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO
ALTER TABLE [dbo].[UserSettings]  WITH CHECK ADD  CONSTRAINT [FK_me_UserSettings_me_Users] FOREIGN KEY([UserId])
REFERENCES [dbo].[Users] ([Id])
GO
ALTER TABLE [dbo].[UserSettings] CHECK CONSTRAINT [FK_me_UserSettings_me_Users]
GO

/*---------------------------------------------------------------------------------------------------------------------------------------------------------------*/
/*-----------------------------------------------------------------------[Skins]------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------------------------------------------------------------------------------------------*/

CREATE TABLE [dbo].[Skins](
	[Id] [int] IDENTITY(1,1) NOT NULL,
	[Name] [nvarchar](255) NOT NULL,
	[SiteId] [int] NOT NULL,
	[CreatedDate] [datetime] NOT NULL,
	[ModifiedDate] [datetime] NOT NULL,
	[CreatedBy] [int] NOT NULL,
	[ModifiedBy] [int] NOT NULL,
 CONSTRAINT [PK_me_Skins] PRIMARY KEY CLUSTERED 
(
	[Id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO
ALTER TABLE [dbo].[Skins]  WITH CHECK ADD  CONSTRAINT [FK_me_Skins_me_Sites] FOREIGN KEY([SiteId])
REFERENCES [dbo].[Sites] ([Id])
GO
ALTER TABLE [dbo].[Skins] CHECK CONSTRAINT [FK_me_Skins_me_Sites]
GO
ALTER TABLE [dbo].[Skins]  WITH CHECK ADD  CONSTRAINT [FK_Skins_Users] FOREIGN KEY([CreatedBy])
REFERENCES [dbo].[Users] ([Id])
GO
ALTER TABLE [dbo].[Skins] CHECK CONSTRAINT [FK_Skins_Users]
GO
ALTER TABLE [dbo].[Skins]  WITH CHECK ADD  CONSTRAINT [FK_Skins_Users1] FOREIGN KEY([ModifiedBy])
REFERENCES [dbo].[Users] ([Id])
GO
ALTER TABLE [dbo].[Skins] CHECK CONSTRAINT [FK_Skins_Users1]
GO

/*---------------------------------------------------------------------------------------------------------------------------------------------------------------*/
/*-----------------------------------------------------------------------[Roles]------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------------------------------------------------------------------------------------------*/

CREATE TABLE [dbo].[Roles](
	[Id] [int] IDENTITY(1,1) NOT NULL,
	[SiteId] [int] NOT NULL,
	[Name] [nvarchar](255) NOT NULL,
	[LoweredName] [nvarchar](255) NOT NULL,
	[Description] [nvarchar](255) NULL,
	[CreatedDate] [datetime] NOT NULL,
	[ModifiedDate] [datetime] NOT NULL,
	[CreatedBy] [int] NOT NULL,
	[ModifiedBy] [int] NOT NULL,
 CONSTRAINT [PK_me_Roles] PRIMARY KEY CLUSTERED 
(
	[Id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO
ALTER TABLE [dbo].[Roles]  WITH CHECK ADD  CONSTRAINT [FK_me_Roles_me_Sites] FOREIGN KEY([SiteId])
REFERENCES [dbo].[Sites] ([Id])
GO
ALTER TABLE [dbo].[Roles] CHECK CONSTRAINT [FK_me_Roles_me_Sites]
GO
ALTER TABLE [dbo].[Roles]  WITH CHECK ADD  CONSTRAINT [FK_Roles_Users] FOREIGN KEY([CreatedBy])
REFERENCES [dbo].[Users] ([Id])
GO
ALTER TABLE [dbo].[Roles] CHECK CONSTRAINT [FK_Roles_Users]
GO
ALTER TABLE [dbo].[Roles]  WITH CHECK ADD  CONSTRAINT [FK_Roles_Users1] FOREIGN KEY([ModifiedBy])
REFERENCES [dbo].[Users] ([Id])
GO
ALTER TABLE [dbo].[Roles] CHECK CONSTRAINT [FK_Roles_Users1]
GO

/*---------------------------------------------------------------------------------------------------------------------------------------------------------------*/
/*-----------------------------------------------------------------------[UsersInRoles]------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------------------------------------------------------------------------------------------*/

CREATE TABLE [dbo].[UsersInRoles](
	[UserId] [int] NOT NULL,
	[RoleId] [int] NOT NULL
) ON [PRIMARY]

GO
ALTER TABLE [dbo].[UsersInRoles]  WITH CHECK ADD  CONSTRAINT [FK_me_UsersInRoles_me_Roles] FOREIGN KEY([RoleId])
REFERENCES [dbo].[Roles] ([Id])
GO
ALTER TABLE [dbo].[UsersInRoles] CHECK CONSTRAINT [FK_me_UsersInRoles_me_Roles]
GO
ALTER TABLE [dbo].[UsersInRoles]  WITH CHECK ADD  CONSTRAINT [FK_me_UsersInRoles_me_Users] FOREIGN KEY([UserId])
REFERENCES [dbo].[Users] ([Id])
GO
ALTER TABLE [dbo].[UsersInRoles] CHECK CONSTRAINT [FK_me_UsersInRoles_me_Users]
GO

/*---------------------------------------------------------------------------------------------------------------------------------------------------------------*/
/*-----------------------------------------------------------------------[Pages]------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------------------------------------------------------------------------------------------*/
CREATE TABLE [dbo].[Pages](
	[Id] [int] IDENTITY(1,1) NOT NULL,
	[PageId] [uniqueidentifier] NOT NULL,
	[Name] [nvarchar](255) NOT NULL,
	[ParentPageId] [int] NULL,
	[SkinId] [int] NULL,
	[SiteId] [int] NOT NULL,
	[Title] [nvarchar](255) NOT NULL,
	[MenuTitle] [nvarchar](255) NULL,
	[Alias] [nvarchar](255) NULL,
	[Keywords] [nvarchar](max) NULL,
	[Description] [nvarchar](max) NULL,
	[CreatedDate] [datetime] NOT NULL,
	[ModifiedDate] [datetime] NOT NULL,
	[CreatedBy] [int] NOT NULL,
	[ModifiedBy] [int] NOT NULL,
 CONSTRAINT [PK_me_Pages] PRIMARY KEY CLUSTERED 
(
	[Id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY],
 CONSTRAINT [IX_Pages] UNIQUE NONCLUSTERED 
(
	[PageId] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO
ALTER TABLE [dbo].[Pages]  WITH CHECK ADD  CONSTRAINT [FK_me_Pages_me_Sites] FOREIGN KEY([SiteId])
REFERENCES [dbo].[Sites] ([Id])
GO
ALTER TABLE [dbo].[Pages] CHECK CONSTRAINT [FK_me_Pages_me_Sites]
GO
ALTER TABLE [dbo].[Pages]  WITH CHECK ADD  CONSTRAINT [FK_Pages_Skins] FOREIGN KEY([SkinId])
REFERENCES [dbo].[Skins] ([Id])
GO
ALTER TABLE [dbo].[Pages] CHECK CONSTRAINT [FK_Pages_Skins]
GO
ALTER TABLE [dbo].[Pages]  WITH CHECK ADD  CONSTRAINT [FK_Pages_Users] FOREIGN KEY([CreatedBy])
REFERENCES [dbo].[Users] ([Id])
GO
ALTER TABLE [dbo].[Pages] CHECK CONSTRAINT [FK_Pages_Users]
GO
ALTER TABLE [dbo].[Pages]  WITH CHECK ADD  CONSTRAINT [FK_Pages_Users1] FOREIGN KEY([ModifiedBy])
REFERENCES [dbo].[Users] ([Id])
GO
ALTER TABLE [dbo].[Pages] CHECK CONSTRAINT [FK_Pages_Users1]
GO

/*---------------------------------------------------------------------------------------------------------------------------------------------------------------*/
/*-----------------------------------------------------------------------[PagePermissions]------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------------------------------------------------------------------------------------------*/
CREATE TABLE [dbo].[PagePermissions](
	[Id] [int] IDENTITY(1,1) NOT NULL,
	[PageId] [int] NOT NULL,
	[RoleId] [int] NOT NULL,
	[AllowModify] [bit] NOT NULL,
	[AllowView] [bit] NOT NULL,
	[CreatedDate] [datetime] NOT NULL,
	[ModifiedDate] [datetime] NOT NULL,
	[CreatedBy] [int] NOT NULL,
	[ModifiedBy] [int] NOT NULL,
 CONSTRAINT [PK_me_PagePermissions_1] PRIMARY KEY CLUSTERED 
(
	[Id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO
ALTER TABLE [dbo].[PagePermissions]  WITH CHECK ADD  CONSTRAINT [FK_me_PagePermissions_me_Roles] FOREIGN KEY([RoleId])
REFERENCES [dbo].[Roles] ([Id])
GO
ALTER TABLE [dbo].[PagePermissions] CHECK CONSTRAINT [FK_me_PagePermissions_me_Roles]
GO
ALTER TABLE [dbo].[PagePermissions]  WITH CHECK ADD  CONSTRAINT [FK_PagePermissions_Users] FOREIGN KEY([CreatedBy])
REFERENCES [dbo].[Users] ([Id])
GO
ALTER TABLE [dbo].[PagePermissions] CHECK CONSTRAINT [FK_PagePermissions_Users]
GO
ALTER TABLE [dbo].[PagePermissions]  WITH CHECK ADD  CONSTRAINT [FK_PagePermissions_Users1] FOREIGN KEY([ModifiedBy])
REFERENCES [dbo].[Users] ([Id])
GO
ALTER TABLE [dbo].[PagePermissions] CHECK CONSTRAINT [FK_PagePermissions_Users1]
GO

/*---------------------------------------------------------------------------------------------------------------------------------------------------------------*/
/*-----------------------------------------------------------------------[Modules]------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------------------------------------------------------------------------------------------*/
CREATE TABLE [dbo].[Modules](
	[Id] [int] IDENTITY(1,1) NOT NULL,
	[Name] [nvarchar](255) NOT NULL,
	[Description] [nvarchar](255) NULL,
	[Version] [nvarchar](50) NOT NULL,
	[ModuleServiceType] [nvarchar](255) NOT NULL,
	[CreatedDate] [datetime] NOT NULL,
	[CreatedBy] [int] NOT NULL,
	[ModifiedDate] [datetime] NOT NULL,
	[ModifiedBy] [int] NOT NULL,
 CONSTRAINT [PK_me_Modules] PRIMARY KEY CLUSTERED 
(
	[Id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO
ALTER TABLE [dbo].[Modules]  WITH CHECK ADD  CONSTRAINT [FK_me_Modules_me_Users] FOREIGN KEY([CreatedBy])
REFERENCES [dbo].[Users] ([Id])
GO
ALTER TABLE [dbo].[Modules] CHECK CONSTRAINT [FK_me_Modules_me_Users]
GO
ALTER TABLE [dbo].[Modules]  WITH CHECK ADD  CONSTRAINT [FK_me_Modules_me_Users1] FOREIGN KEY([ModifiedBy])
REFERENCES [dbo].[Users] ([Id])
GO
ALTER TABLE [dbo].[Modules] CHECK CONSTRAINT [FK_me_Modules_me_Users1]
GO

/*---------------------------------------------------------------------------------------------------------------------------------------------------------------*/
/*-----------------------------------------------------------------------[Controls]------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------------------------------------------------------------------------------------------*/
CREATE TABLE [dbo].[Controls](
	[Id] [uniqueidentifier] NOT NULL,
	[Name] [nvarchar](255) NOT NULL,
	[ControlKey] [nvarchar](50) NOT NULL,
	[OnePerPage] [bit] NOT NULL,
	[ModuleId] [int] NOT NULL,
	[Description] [nvarchar](255) NULL,
	[ControllerType] [nvarchar](255) NOT NULL,
	[DefaultAction] [nvarchar](50) NOT NULL,
	[CreatedDate] [datetime] NOT NULL,
	[CreatedBy] [int] NOT NULL,
	[ModifiedDate] [datetime] NOT NULL,
	[ModifiedBy] [int] NOT NULL,
 CONSTRAINT [PK_Controls] PRIMARY KEY CLUSTERED 
(
	[Id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO
ALTER TABLE [dbo].[Controls]  WITH CHECK ADD  CONSTRAINT [FK_Controls_Modules] FOREIGN KEY([ModuleId])
REFERENCES [dbo].[Modules] ([Id])
GO
ALTER TABLE [dbo].[Controls] CHECK CONSTRAINT [FK_Controls_Modules]
GO
ALTER TABLE [dbo].[Controls]  WITH CHECK ADD  CONSTRAINT [FK_Controls_Users] FOREIGN KEY([CreatedBy])
REFERENCES [dbo].[Users] ([Id])
GO
ALTER TABLE [dbo].[Controls] CHECK CONSTRAINT [FK_Controls_Users]
GO
ALTER TABLE [dbo].[Controls]  WITH CHECK ADD  CONSTRAINT [FK_Controls_Users1] FOREIGN KEY([ModifiedBy])
REFERENCES [dbo].[Users] ([Id])
GO
ALTER TABLE [dbo].[Controls] CHECK CONSTRAINT [FK_Controls_Users1]
GO

/*---------------------------------------------------------------------------------------------------------------------------------------------------------------*/
/*-----------------------------------------------------------------------[Containers]------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------------------------------------------------------------------------------------------*/
CREATE TABLE [dbo].[Containers](
	[Id] [int] IDENTITY(1,1) NOT NULL,
	[SkinId] [int] NOT NULL,
	[Title] [nvarchar](50) NOT NULL,
	[MarkupId] [nvarchar](50) NOT NULL,
	[CreatedDate] [datetime] NOT NULL,
	[ModifiedDate] [datetime] NOT NULL,
	[CreatedBy] [int] NOT NULL,
	[ModifiedBy] [int] NOT NULL,
 CONSTRAINT [PK_ContentPanels] PRIMARY KEY CLUSTERED 
(
	[Id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO
ALTER TABLE [dbo].[Containers]  WITH CHECK ADD  CONSTRAINT [FK_Containers_Users] FOREIGN KEY([CreatedBy])
REFERENCES [dbo].[Users] ([Id])
GO
ALTER TABLE [dbo].[Containers] CHECK CONSTRAINT [FK_Containers_Users]
GO
ALTER TABLE [dbo].[Containers]  WITH CHECK ADD  CONSTRAINT [FK_Containers_Users1] FOREIGN KEY([ModifiedBy])
REFERENCES [dbo].[Users] ([Id])
GO
ALTER TABLE [dbo].[Containers] CHECK CONSTRAINT [FK_Containers_Users1]
GO
ALTER TABLE [dbo].[Containers]  WITH CHECK ADD  CONSTRAINT [FK_me_Containers_me_Skins] FOREIGN KEY([SkinId])
REFERENCES [dbo].[Skins] ([Id])
GO
ALTER TABLE [dbo].[Containers] CHECK CONSTRAINT [FK_me_Containers_me_Skins]
GO

/*---------------------------------------------------------------------------------------------------------------------------------------------------------------*/
/*-----------------------------------------------------------------------[ControlAssistances]------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------------------------------------------------------------------------------------------*/
CREATE TABLE [dbo].[ControlAssistances](
	[Id] [uniqueidentifier] NOT NULL,
	[ContainerId] [int] NOT NULL,
	[PageId] [uniqueidentifier] NOT NULL,
	[ControlId] [uniqueidentifier] NOT NULL,
	[CreatedDate] [datetime] NOT NULL,
	[ModifiedDate] [datetime] NOT NULL,
	[CreatedBy] [int] NOT NULL,
	[ModifiedBy] [int] NOT NULL,
 CONSTRAINT [PK_ControlAssistances] PRIMARY KEY CLUSTERED 
(
	[Id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY],
 CONSTRAINT [IX_ControlAssistances] UNIQUE NONCLUSTERED 
(
	[ContainerId] ASC,
	[ControlId] ASC,
	[PageId] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO
ALTER TABLE [dbo].[ControlAssistances]  WITH CHECK ADD  CONSTRAINT [FK_ControlAssistances_Containers] FOREIGN KEY([ContainerId])
REFERENCES [dbo].[Containers] ([Id])
GO
ALTER TABLE [dbo].[ControlAssistances] CHECK CONSTRAINT [FK_ControlAssistances_Containers]
GO
ALTER TABLE [dbo].[ControlAssistances]  WITH CHECK ADD  CONSTRAINT [FK_ControlAssistances_Controls] FOREIGN KEY([ControlId])
REFERENCES [dbo].[Controls] ([Id])
GO
ALTER TABLE [dbo].[ControlAssistances] CHECK CONSTRAINT [FK_ControlAssistances_Controls]
GO
ALTER TABLE [dbo].[ControlAssistances]  WITH CHECK ADD  CONSTRAINT [FK_ControlAssistances_Pages] FOREIGN KEY([PageId])
REFERENCES [dbo].[Pages] ([PageId])
GO
ALTER TABLE [dbo].[ControlAssistances] CHECK CONSTRAINT [FK_ControlAssistances_Pages]
GO
ALTER TABLE [dbo].[ControlAssistances]  WITH CHECK ADD  CONSTRAINT [FK_ControlAssistances_Users] FOREIGN KEY([CreatedBy])
REFERENCES [dbo].[Users] ([Id])
GO
ALTER TABLE [dbo].[ControlAssistances] CHECK CONSTRAINT [FK_ControlAssistances_Users]
GO
ALTER TABLE [dbo].[ControlAssistances]  WITH CHECK ADD  CONSTRAINT [FK_ControlAssistances_Users1] FOREIGN KEY([ModifiedBy])
REFERENCES [dbo].[Users] ([Id])
GO
ALTER TABLE [dbo].[ControlAssistances] CHECK CONSTRAINT [FK_ControlAssistances_Users1]
GO

/*---------------------------------------------------------------------------------------------------------------------------------------------------------------*/
/*-----------------------------------------------------------------------[PageContents]------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------------------------------------------------------------------------------------------*/
CREATE TABLE [dbo].[PageContents](
	[Id] [int] IDENTITY(1,1) NOT NULL,
	[SiteId] [int] NOT NULL,
	[PageId] [int] NOT NULL,
	[ContainerId] [int] NOT NULL,
	[ControlId] [uniqueidentifier] NOT NULL,
	[Position] [int] NOT NULL,
	[AlwaysVisible] [bit] NOT NULL,
	[CreatedDate] [datetime] NOT NULL,
	[ModifiedDate] [datetime] NOT NULL,
	[CreatedBy] [int] NOT NULL,
	[ModifiedBy] [int] NOT NULL,
 CONSTRAINT [PK_me_PageContents] PRIMARY KEY CLUSTERED 
(
	[Id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO
ALTER TABLE [dbo].[PageContents]  WITH CHECK ADD  CONSTRAINT [FK_me_PageContents_me_Containers] FOREIGN KEY([ContainerId])
REFERENCES [dbo].[Containers] ([Id])
GO
ALTER TABLE [dbo].[PageContents] CHECK CONSTRAINT [FK_me_PageContents_me_Containers]
GO
ALTER TABLE [dbo].[PageContents]  WITH CHECK ADD  CONSTRAINT [FK_me_PageContents_me_Sites] FOREIGN KEY([SiteId])
REFERENCES [dbo].[Sites] ([Id])
GO
ALTER TABLE [dbo].[PageContents] CHECK CONSTRAINT [FK_me_PageContents_me_Sites]
GO
ALTER TABLE [dbo].[PageContents]  WITH CHECK ADD  CONSTRAINT [FK_PageContents_Controls] FOREIGN KEY([ControlId])
REFERENCES [dbo].[Controls] ([Id])
GO
ALTER TABLE [dbo].[PageContents] CHECK CONSTRAINT [FK_PageContents_Controls]
GO
ALTER TABLE [dbo].[PageContents]  WITH CHECK ADD  CONSTRAINT [FK_PageContents_Pages] FOREIGN KEY([PageId])
REFERENCES [dbo].[Pages] ([Id])
GO
ALTER TABLE [dbo].[PageContents] CHECK CONSTRAINT [FK_PageContents_Pages]
GO
ALTER TABLE [dbo].[PageContents]  WITH CHECK ADD  CONSTRAINT [FK_PageContents_Users] FOREIGN KEY([CreatedBy])
REFERENCES [dbo].[Users] ([Id])
GO
ALTER TABLE [dbo].[PageContents] CHECK CONSTRAINT [FK_PageContents_Users]
GO
ALTER TABLE [dbo].[PageContents]  WITH CHECK ADD  CONSTRAINT [FK_PageContents_Users1] FOREIGN KEY([ModifiedBy])
REFERENCES [dbo].[Users] ([Id])
GO
ALTER TABLE [dbo].[PageContents] CHECK CONSTRAINT [FK_PageContents_Users1]
GO

/*---------------------------------------------------------------------------------------------------------------------------------------------------------------*/
/*-----------------------------------------------------------------------[ActionPermissions]------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------------------------------------------------------------------------------------------*/
CREATE TABLE [dbo].[ActionPermissions](
	[Id] [int] IDENTITY(1,1) NOT NULL,
	[Controller] [nvarchar](255) NOT NULL,
	[Action] [nvarchar](255) NOT NULL,
	[RoleId] [int] NOT NULL,
	[CreatedDate] [datetime] NOT NULL,
	[ModifiedDate] [datetime] NOT NULL,
 CONSTRAINT [PK_me_PagePermissions] PRIMARY KEY CLUSTERED 
(
	[Id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO
ALTER TABLE [dbo].[ActionPermissions]  WITH CHECK ADD  CONSTRAINT [FK_me_ActionPermissions_me_Roles] FOREIGN KEY([RoleId])
REFERENCES [dbo].[Roles] ([Id])
GO
ALTER TABLE [dbo].[ActionPermissions] CHECK CONSTRAINT [FK_me_ActionPermissions_me_Roles]
GO

/*---------------------------------------------------------------------------------------------------------------------------------------------------------------*/
/*-----------------------------------------------------------------------[Logs]------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------------------------------------------------------------------------------------------*/
CREATE TABLE [dbo].[Logs](
	[Id] [int] IDENTITY(1,1) NOT NULL,
	[SiteId] [int] NOT NULL,
	[CreatedDate] [datetime] NOT NULL,
	[UserId] [int] NULL,
	[Type] [nvarchar](255) NOT NULL,
	[Message] [nvarchar](max) NOT NULL,
	[Exception] [nvarchar](max) NULL,
 CONSTRAINT [PK_me_Log] PRIMARY KEY CLUSTERED 
(
	[Id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO
ALTER TABLE [dbo].[Logs]  WITH CHECK ADD  CONSTRAINT [FK_me_Logs_me_Sites] FOREIGN KEY([SiteId])
REFERENCES [dbo].[Sites] ([Id])
GO
ALTER TABLE [dbo].[Logs] CHECK CONSTRAINT [FK_me_Logs_me_Sites]
GO
ALTER TABLE [dbo].[Logs]  WITH CHECK ADD  CONSTRAINT [FK_me_Logs_me_Users] FOREIGN KEY([UserId])
REFERENCES [dbo].[Users] ([Id])
GO
ALTER TABLE [dbo].[Logs] CHECK CONSTRAINT [FK_me_Logs_me_Users]
GO

/*---------------------------------------------------------------------------------------------------------------------------------------------------------------*/
/*-----------------------------------------------------------------------[PageContentDetails]------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------------------------------------------------------------------------------------------*/
CREATE VIEW [dbo].[PageContentDetails]
AS
SELECT     dbo.PageContents.Id, dbo.PageContents.PageId, dbo.PageContents.ContainerId, dbo.PageContents.ControlId, dbo.Containers.MarkupId AS ContainerMarkupId, 
                      dbo.PageContents.Position, dbo.PageContents.SiteId, dbo.Controls.Name AS ControlName, dbo.PageContents.AlwaysVisible
FROM         dbo.PageContents INNER JOIN
                      dbo.Containers ON dbo.PageContents.ContainerId = dbo.Containers.Id INNER JOIN
                      dbo.Pages ON dbo.PageContents.PageId = dbo.Pages.Id INNER JOIN
                      dbo.Controls ON dbo.PageContents.ControlId = dbo.Controls.Id

GO

/*---------------------------------------------------------------------------------------------------------------------------------------------------------------*/
/*-----------------------------------------------------------------------DATA------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------------------------------------------------------------------------------------------*/
INSERT INTO Sites VALUES('DefaultSite', 'test', '', 1, 'Default', 0, 'uk-UA', GetDate(), GetDate())
INSERT INTO Sites VALUES('SubSite', 'test', 'SubSite', 2, 'Default', 0, 'en-US', GetDate(), GetDate())
GO
INSERT INTO Users VALUES(1, 'aa@aa.ru', 1, 'N0dKIr/1uF0a3XqdsVeD7Q==', 'K1SxanJVtlU+/x+ED1KB1w==', 'aa@aa.ru', 1, 0, GetDate(), GetDate(), 0, 0, NULL, NULL, NULL, NULL)
GO

INSERT INTO Roles VALUES(1, 'Administrators', 'administrators', NULL, GetDate(), GetDate(), 1, 1)
INSERT INTO Roles VALUES(1, 'Unregistered Users', 'unregistered users', NULL, GetDate(), GetDate(), 1, 1)
INSERT INTO Roles VALUES(1, 'Users', 'users', NULL, GetDate(), GetDate(), 1, 1)
INSERT INTO Roles VALUES(2, 'Administrators', 'administrators', NULL, GetDate(), GetDate(), 1, 1)
INSERT INTO Roles VALUES(2, 'Unregistered Users', 'unregistered users', NULL, GetDate(), GetDate(), 1, 1)
INSERT INTO Roles VALUES(2, 'Users', 'users', NULL, GetDate(), GetDate(), 1, 1)
GO

INSERT INTO UsersInRoles VALUES(1, 1)
GO

INSERT INTO Skins VALUES('Default', 1, GetDate(), GetDate(), 1, 1)
INSERT INTO Skins VALUES('Default', 2, GetDate(), GetDate(), 1, 1)
INSERT INTO Skins VALUES('iTheme', 1, GetDate(), GetDate(), 1, 1)
GO

INSERT INTO Pages VALUES('6126396c-dc7b-46e4-b26b-2593bf8f21cd', 'Home', NULL, NULL, 1, '$HomePageTitle', '$HomePageMenuTitle', 'Home', '$HomePageKeywords', '$HomePageDescription', GetDate(), GetDate(), 1, 1)
INSERT INTO Pages VALUES('b5fea60d-4feb-41b4-bbc2-10e538229054', 'Home', NULL, NULL, 2, '$HomePageTitle', '$HomePageMenuTitle', 'Home', '$HomePageKeywords', '$HomePageDescription', GetDate(), GetDate(), 1, 1)
GO

INSERT INTO Containers VALUES(1, 'TopPane', 'TopPane', GetDate(), GetDate(), 1, 1)
INSERT INTO Containers VALUES(1, 'LeftPane', 'LeftPane', GetDate(), GetDate(), 1, 1)
INSERT INTO Containers VALUES(1, 'RightPane', 'RightPane', GetDate(), GetDate(), 1, 1)
INSERT INTO Containers VALUES(1, 'ContentPane', 'ContentPane', GetDate(), GetDate(), 1, 1)
INSERT INTO Containers VALUES(1, 'BottomPane', 'BottomPane', GetDate(), GetDate(), 1, 1)
INSERT INTO Containers VALUES(3, 'LeftPane', 'LeftPane', GetDate(), GetDate(), 1, 1)
INSERT INTO Containers VALUES(3, 'RightPane', 'RightPane', GetDate(), GetDate(), 1, 1)
GO
